﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Utilities;
using HTTT.DataAccess;
using HTTT.Bussiness;

namespace HTTT.admin.htoperator
{
    public partial class AddOperator : System.Web.UI.Page
    {
        /// <summary>
        ///  View State AddItemID
        /// </summary>
        /// 
        private Guid? AddItemID
        {
            get
            {
                object obj = this.ViewState["_AddItemID"];
                if (obj == null)
                    return null; //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_AddItemID"] = value;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                CheckPermission();
            }
        }

        private void LoadData()
        {
            
        }

        private void CheckPermission()
        {
            if (!Page.User.IsInRole(Constants.ROLE_SUPER_ADMINISTRATOR)
                 && !Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR))
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ReturnValue<bool> Valid = ValidateData();
            if (Valid.RetValue)
            {
                SaveForm();
                //TODO: Show msg
                Response.Redirect("EditOperator.aspx?ID=" + AddItemID);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg(Valid.Msg), true);
            }

        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            ReturnValue<bool> Valid = ValidateData();
            if (Valid.RetValue)
            {
                bool result = SaveForm();
                if (result)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatMsgThenRedirect("Thêm mới  thành công", "OperatorList.aspx"), true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatAlertMsg("Có lỗi xảy ra"), true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg(Valid.Msg), true);
            }

        }
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            ReturnValue<bool> Valid = ValidateData();
            if (Valid.RetValue)
            {
                SaveForm();
                AddItemID = null;
                Response.Redirect("AddOperator.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg(Valid.Msg), true);
            }

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("OperatorList.aspx");
        }

        private ReturnValue<bool> ValidateData()
        {
            if (txtOrganize.Text.Trim() == string.Empty) return new ReturnValue<bool>(false, "Phải nhập tên tổ chức");
            if (txtOName.Text.Trim() == string.Empty) return new ReturnValue<bool>(false, "Phải nhập tên quản trị mạng");
            return new ReturnValue<bool>(true, "");
        }


        private bool SaveForm()
        {
            //Collec infomation
            HT_Operator NewOperator = new HT_Operator();
            NewOperator.ID = Guid.NewGuid();
            NewOperator.Organize = txtOrganize.Text;
            NewOperator.Speed = txtOSpeed.Text;
            NewOperator.Ip = txtOIp.Text;
            NewOperator.OperatorName = txtOName.Text;
            NewOperator.OperatorMobile = txtOMobile.Text;
            NewOperator.OperatorPhone = txtOPhone.Text;
            NewOperator.OperatorEmail = txtOEmail.Text;
            NewOperator.Account = txtAccount.Text;
            AddItemID = NewOperator.ID;
            HT_OperatorBO re = new HT_OperatorBO();
            bool result = re.InsertHT_Operator(NewOperator);
            return result;
        }
    }
}